-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: .shift optionally takes multiple periods #54115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: .shift optionally takes multiple periods #54115
Conversation
I think the pyarrow failure is unrelated? Otherwise green. |
Ok now it's all green. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is looking good! Some requests/suggestions below.
@@ -3002,7 +3002,7 @@ def autocorr(self, lag: int = 1) -> float: | |||
>>> s.autocorr() | |||
nan | |||
""" | |||
return self.corr(self.shift(lag)) | |||
return self.corr(cast(Series, self.shift(lag))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary because Series.shift can now return a dataframe if there are multiple periods. Mypy complains at this line because it wants Series and not Series | Dataframe
, but because there is only one lag, it's always Series
, so we can just cast
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add type-overloads to handle this. Sometimes they can get gnarly (see e.g. concat), but I think this one should be relatively straight forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to if that's ok? There is no Series.shift
to overload so I feel like it would be confusing to newcomers like me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you insist I will try!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise I think this is done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rhshadrach , do you have time for another round/sign this off?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with opening an issue regarding this once it's merged for someone to followup on.
All green! |
@@ -3002,7 +3002,7 @@ def autocorr(self, lag: int = 1) -> float: | |||
>>> s.autocorr() | |||
nan | |||
""" | |||
return self.corr(self.shift(lag)) | |||
return self.corr(cast(Series, self.shift(lag))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with opening an issue regarding this once it's merged for someone to followup on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just about there; one last request I think.
@rhshadrach @mroeschke I originally wanted this functionality to build simple Almon distributed lags models (or the corresponding thing from cognitive neuroscience), would there be a room for a Pandas docs example for this? Basically showing how an impulse response function can be recovered via regression of a multi-lagged feature. E: maybe this PR isn't the space for this, I can also move to an issue |
… into jona/44424_shift
Done - @rhshadrach everything ok from your end? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Thanks @jona-sassenhagen! Great work! |
Thanks @rhshadrach ! May I re-raise this: #54115 (comment) |
I recommend making an issue. There a few places that come to my mind:
A user guide example would need to be (a) self-contained and (b) not too long to be appropriate; the cookbook should be for generic operations (no domain context); and the ecosystem for packages. I'm not sure exactly what you're thinking of, but it sounds like it may not fit in any of these. But we can discuss further in an issue. |
Thanks, I will do that. |
Taking over from https://github.com/pandas-dev/pandas/pull/44660/files
doc/source/whatsnew/v2.1.0.rst
file if fixing a bug or adding a new feature.